home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / FCOPY31.ARJ / FCOPY.C < prev    next >
C/C++ Source or Header  |  1992-05-12  |  8KB  |  124 lines

  1. /*  FCOPY.C  --  contains fcopy() function                              */
  2. /*  Version 2.1                                                         */
  3. /*                                                                      */
  4. /*  This function copies one file to another, much as the DOS COPY      */
  5. /*  command does.  It does not check to see if the two files are        */
  6. /*  the same, so it is up to the caller to verify that the two file     */
  7. /*  specs do not, in fact, refer to the same file.                      */
  8. /*                                                                      */
  9. /*  fcopy() uses a farmalloc() call to allocate a large file buffer     */
  10. /*  from the far heap, regardless of the memory model being used.  The  */
  11. /*  actual copy operation is performed in the assembler function        */
  12. /*  _copyfile().  The source code for this module is contained in       */
  13. /*  COPYFILE.ASM.  This function is a little smaller and slightly       */
  14. /*  faster than using the separate calls to _farread() and _farwrite()  */
  15. /*  functions (as was done in version 1.1), since the far pointer is    */
  16. /*  only loaded once with _copyfile().                                  */
  17. /*                                                                      */
  18. /*  BUFF_SIZE, indicating the buffer size, can be any int sized         */
  19. /*  value up to (but not including) 65,535.  It is recommended that     */
  20. /*  the buffer be a multiple of the sector size (512 bytes in the       */
  21. /*  current version of DOS), since reads and writes are more efficient  */
  22. /*  if they involve whole sectors.  A good value to use is 65024 (in    */
  23. /*  decimal), since this is 1 sector (512 bytes) short of a complete    */
  24. /*  segment (64k bytes).                                                */
  25. /*                                                                      */
  26. /*  Function prototypes are contained in fcopy.h                        */
  27. /*                                                                      */
  28. /*  Usage:                                                              */
  29. /*                                                                      */
  30. /*      #include "fcopy.h"                                              */
  31. /*      int fcopy (char *sourcename, char *targetname)                  */
  32. /*                                                                      */
  33. /*  The function returns 0 if sucessful, -1 otherwise.  Error codes     */
  34. /*  are returned in the global variables errno and _doserrno.           */
  35. /*  Possible error codes are:                                           */
  36. /*                                                                      */
  37. /*      EINVFNC     Invalid function number  (1)                        */
  38. /*      ENOFILE     File not found  (2)                                 */
  39. /*      ENOPATH     Path not found  (3)                                 */
  40. /*      EMFILE      Too many open files  (4)                            */
  41. /*      EACCESS     Permission denied  (5)                              */
  42. /*      EBADF       Bad file number  (6)                                */
  43. /*      ECONTR      Memory blocks destroyed  (7)                        */
  44. /*      ENOMEM      Not enough core  (8)                                */
  45. /*      EINVMEM     Invalid memory block address  (9)                   */
  46. /*      EINVACC     Invalid access code  (12)                           */
  47. /*           -2     Target disk full  (-2)                              */
  48. /*  ------------------------------------------------------------------- */
  49. /*  Revision history:                                                   */
  50. /*                                                                      */
  51. /*      1.0      5 JAN 92       Original.  Written for Turbo C++.       */
  52. /*                                                                      */
  53. /*      1.1     19 JAN 92       Updated comments to reflect _farread()  */
  54. /*                              and _farwrite() being split into        */
  55. /*                              separate modules.  No code changes to   */
  56. /*                              this module.                            */
  57. /*                                                                      */
  58. /*      2.0     16 MAR 92       Replaced _farread() and _farwrite()     */
  59. /*                              functions with a single _copyfile()     */
  60. /*                              function.  fcopy() modified to match.   */
  61. /*                                                                      */
  62. /*      2.1     12 MAY 92       Dropped the leading underscore from     */
  63. /*                              the function name; it is now fcopy      */
  64. /*                              (rather than _fcopy).                   */
  65. /*  ------------------------------------------------------------------- */
  66. /*      Copyright (c) 1992 Ray Waters                                   */
  67. /*      All Rights Reserved                                             */
  68. /*  ------------------------------------------------------------------- */
  69.  
  70. #define BUFF_SIZE   65024U          /* size of file buffer */
  71.  
  72. #include <io.h>             /* _open, _creat, _close */
  73. #include <fcntl.h>          /* definition for O_RDONLY */
  74. #include <alloc.h>          /* farmalloc, farfree */
  75. #include "fcopy.h"          /* fcopy, _copyfile */
  76.  
  77. void __cleanup(void);       /* function to close files, free memory */
  78.  
  79. char far *buffer;               /* far pointer to the file buffer */
  80. int  SrcHandle, TgtHandle;      /* file handles for source and target */
  81.  
  82. int fcopy(char *sourcename, char *targetname)
  83. {
  84.     struct ftime ft;                /* structure for file date/time stamp */
  85.  
  86.     SrcHandle = _open(sourcename, O_RDONLY);    /* open source (read only) */
  87.     if (SrcHandle == -1)                        /* if open failed, */
  88.         return -1;                              /* return error */
  89.     if (getftime(SrcHandle, &ft)) {             /* get date/time stamp */
  90.         _close(SrcHandle);                      /* if error, close file */
  91.         return -1;                              /* return error */
  92.     }
  93.  
  94.     TgtHandle = _creat(targetname, 0);          /* create/truncate target */
  95.     if (TgtHandle == -1) {                      /* if open failed, */
  96.         _close(SrcHandle);                      /* close source file */
  97.         return -1;                              /* return error */
  98.     }
  99.  
  100.     if  (!(buffer = farmalloc(BUFF_SIZE))) {    /* allocate a far buffer */
  101.                                                 /* if allocation failed, */
  102.         _close(SrcHandle);                      /* close source file */
  103.         _close(TgtHandle);                      /* close target file */
  104.         return -1;                              /* return error */
  105.     }
  106.  
  107.                                                 /* perform the copy */
  108.     if (_copyfile(SrcHandle, TgtHandle, buffer, BUFF_SIZE)) {
  109.         __cleanup();                /* if copy failed, close files, free mem */
  110.         return -1;                              /* return error */
  111.     }
  112.  
  113.     setftime(TgtHandle, &ft);               /* set target date/time stamp */
  114.     __cleanup();                                /* close files, free mem */
  115.     return 0;                                   /* return success */
  116. }
  117.  
  118. void __cleanup(void)                    /* close files and release memory */
  119. {
  120.     _close(SrcHandle);                          /* close source file */
  121.     _close(TgtHandle);                          /* close target file */
  122.     farfree(buffer);                            /* free memory */
  123. }
  124.